package com.caigou.service.impl;


import com.caigou.bean.cms.entity.Us;
import com.caigou.bean.cms.param.UsParam;
import com.caigou.component.HtmlBuilderComponent;
import com.caigou.mapper.cms.UsMapper;
import com.caigou.service.UsService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("usService")
public class UsServiceImpl implements UsService {

    @Resource
    private UsMapper usMapper;

    @Resource
    private HtmlBuilderComponent htmlBuilderComponent;

    public Us selectByPrimaryKey(Integer id){
        return usMapper.selectByPrimaryKey(id);
    }

    public int deleteByPrimaryKey(Integer id){
        return usMapper.deleteByPrimaryKey(id);
    }

    public List<Us> listUs(){
        return usMapper.listUs();
    }

    public int insertSelective(Us us){
        return usMapper.insertSelective(us);
    }

    public int updateUs(Us us){
        return usMapper.updateUs(us);
    }

    public Result saveAndPublishUs(UsParam param){
        Us us = usMapper.selectByPrimaryKey(param.getId());
        if(us==null){
            return Result.error("没有这个’我们‘页面");
        }
        us.setUsImage(param.getUsImage());
        us.setUsContent(param.getUsContent());
        us.setUpdateTime(new Date(System.currentTimeMillis()));
        int result = usMapper.updateUs(us);
        if(result>0){
            //生成静态页面
            String url = htmlBuilderComponent.buildUsHtml(param.getId());
            if(url!=null) {
                us.setUsUrl(url);
                usMapper.updateUs(us);
                return Result.success("保存发布“我们”页面成功");
            }else{
                return Result.error("发布“我们”页面失败");
            }
        }else{
            return Result.error("保存发布“我们”页面失败");
        }
    }
}
